<?php
include_once '../../public/inc/DB.php';
$db = new DB('jusmor_news_cate');

$action = $_POST['action'];

$return = array('code'=>0, 'msg'=>'');
$now = date('Y-m-d H:i:s');

/**
 * 添加分类
 */
if ($action == 'addcate') {
	$news_cate_name = $_POST['cate_name'];
	
	if(empty($news_cate_name)) {
		$return['msg'] = '分类名称不能为空！';
	} else {
		
		//查询该分类是否已经添加
		$condition = array('news_cate_name'=>$news_cate_name);
		$res_count = $db->fetchCount($condition);
		if($res_count > 0) {
			$return['msg'] = '该分类已经添加！';
		} else {
	 		//添加新分类
			$data = array('news_cate_name'=>$news_cate_name, 'create_time'=>$now, 'update_time'=>$now);
			$result = $db->insert($data);
			if(! empty($result)) {
				$return['code'] = 1;
				$return['msg'] = '添加成功！';
			} else {
				$return['msg'] = '添加失败！';
			}
		}
	}
	echo json_encode($return);
	exit();
}

/**
 * 修改分类
 */
if($action == 'updatecate') {
	$news_cate_name = $_POST['cate_name'];
	$news_cate_id = $_POST['cate_id'];
	
	if(empty($news_cate_id) || empty($news_cate_name)) {
		$return['msg'] = '信息不完整，添加失败！';
	} else {
		$condition = array('news_cate_id'=>$news_cate_id);
		$data = array('news_cate_name'=>$news_cate_name, 'update_time'=>$now);
		$result = $db->update($data, $condition);
		if (! empty($result)) {
			$return['code'] = 1;
			$return['msg'] = '修改成功！';
		} else {
			$return['msg'] = '修改失败！';
		}
	}
	echo json_encode($return);
	exit();
}

/**
 * 删除分类
 */
if($action == 'delcate') {
	$news_cate_id = $_POST['cate_id'];
	if(empty($news_cate_id)) {
		$return['msg'] = '信息不完整，删除失败！';
	} else {
		$condition = array('news_cate_id'=>$news_cate_id);
		//判断该分类下是否有文章，如果有就不可以删除
		$res_count = $db->fetchCount($condition, 'jusmor_news');
		if($res_count > 0) {
			$return['msg'] = '该分类下还有文章，删除失败！';
		} else {
			$result = $db->delete($condition);
			if($result) {
				$return['code'] = 1;
				$return['msg'] = '删除成功！';
			} else {
				$return['msg'] = '未知错误，请刷新重试！';
			}
		}
	}
	echo json_encode($return);
	exit();
}

/**
 * 删除文章
 */
if($action == 'del_news') {
	$news_id = $_POST['news_id'];
	if(empty($news_id)) {
		$return['msg'] = '网络繁忙，删除失败！';
	} else {
		$condition = array('news_id'=>$news_id);
		$result = $db->delete($condition, 'jusmor_news');
		if($result) {
			$return['code'] = 1;
			$return['msg'] = '删除成功！';
		} else {
			$return['msg'] = '未知错误，请刷新重试！';
		}
	}
	echo json_encode($return);
	exit();
}








